package com.agent.service.impl;

import com.agent.mapper.ActivitiesMapper;
import com.agent.entity.Activities;
import com.agent.service.ActivitiesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class ActivitiesServiceImpl extends ServiceImpl<ActivitiesMapper, Activities> implements ActivitiesService {

    @Autowired
    private ActivitiesMapper activitiesMapper;

    // 获取指定日期的指定用户的所有活动
    @Override
    public List<Activities> getActivitiesByDate(Long userId, LocalDate date,LocalTime startTime, LocalTime endTime) {
        return activitiesMapper.findByUserIdAndDate(userId, date,startTime, endTime);
    }

    // 检查活动时间是否冲突
    @Override
    public List<Activities> checkConflicts(Long userId, LocalDate date,
                                           LocalTime startTime, LocalTime endTime) {
        return activitiesMapper.findConflictingActivities(userId, date, startTime, endTime);
    }

    @Override
    public LocalDateTime getCurrentTime() {
        return LocalDateTime.now();
    }

    @Override
    public LocalDate parseRelativeDate(String relativeTime) {
        LocalDate today = LocalDate.now();

        return switch (relativeTime.toLowerCase()) {
            case "今天", "today" -> today;
            case "明天", "tomorrow" -> today.plusDays(1);
            case "后天", "day after tomorrow" -> today.plusDays(2);
            case "下周一", "next monday" -> today.plusDays((8 - today.getDayOfWeek().getValue()) % 7);
            case "下周二", "next tuesday" -> today.plusDays((9 - today.getDayOfWeek().getValue()) % 7);
            case "下周三", "next wednesday" -> today.plusDays((10 - today.getDayOfWeek().getValue()) % 7);
            case "下周四", "next thursday" -> today.plusDays((11 - today.getDayOfWeek().getValue()) % 7);
            case "下周五", "next friday" -> today.plusDays((12 - today.getDayOfWeek().getValue()) % 7);
            case "下周六", "next saturday" -> today.plusDays((13 - today.getDayOfWeek().getValue()) % 7);
            case "下周日", "next sunday" -> today.plusDays((14 - today.getDayOfWeek().getValue()) % 7);
            default -> today; // 默认返回今天
        };
    }
}
